package view;

import model.Model;
import javafx.scene.Cursor;
import javafx.scene.paint.*;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.scene.canvas.*;
import javafx.scene.input.MouseEvent;

/**
 * Instantiation of {@link SimulationView} that is usually coupled with a
 * {@link SimulationViewZoomin} as a zoom side control.
 *
 * @author Ricardo Cruz {@literal <ricardo.pdm.cruz@gmail.com>}
 * @since  2014-11-13
 */
public class SimulationViewZoomout extends SimulationView
{
	// currently, it uses the ratio of the currently defined model (3000x2000)
	private static final int WIDTH = 180, HEIGHT = 120;
	private SimulationViewZoomin zoomin;

	public SimulationViewZoomout(SimulationViewListener listener,
			SimulationViewZoomin zoomin) {
		super(((double)WIDTH)/Model.getPetriWidth());
		this.zoomin = zoomin;
		setMinSize(WIDTH, HEIGHT);
		setMaxSize(WIDTH, HEIGHT);
		setOnMousePressed(ev -> {
			setCursor(Cursor.MOVE);
		});
		setOnMouseReleased(ev -> {
			setCursor(null);
		});
		setOnMousePressed((MouseEvent ev) -> {
			mouse(listener, ev);
		});
		setOnMouseDragged((MouseEvent ev) -> {
			mouse(listener, ev);
		});
		setOnScroll(ev -> {
			listener.zoomSimulation(zoomin.zoom + Math.signum(ev.getDeltaY())/4.);
		});
	}

	protected void draw(GraphicsContext gc, double width, double height) {
		super.draw(gc, width, height);
		gc.beginPath();
		gc.rect(zoomin.x0()*zoom, zoomin.y0()*zoom,
			(zoomin.getWidth()/zoomin.zoom)*zoom,
			(zoomin.getHeight()/zoomin.zoom)*zoom);
		gc.setStroke(Color.gray(0.80));
		gc.setFill(Color.gray(0.80, 0.1));
		gc.stroke();
		gc.fill();

		gc.setFill(Color.gray(0.80));
		gc.setFont(Font.font(9));
		gc.setTextAlign(TextAlignment.RIGHT);
		String size = String.format("%dx%d \u00B5m", Model.getPetriWidth(), Model.getPetriHeight());
		gc.fillText(size, WIDTH, HEIGHT);
	}

	private void mouse(SimulationViewListener listener, MouseEvent ev) {
		zoomin.setFocus(ev.getX()/zoom, ev.getY()/zoom);
		listener.moveSimulation(zoomin.xc, zoomin.yc);
	}
}
